package com.timo.mail.receiv;

import jodd.mail.*;
import org.springframework.util.Assert;

import javax.activation.DataSource;
import java.io.File;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description:
 * @Author: timo
 * @Date: 2019/9/13 22:42
 */
public class PopReceivingMail {

    private Pop3Server pop3Server;
    private ReceiveMailSession session;


    public PopReceivingMail(Pop3Server pop3Server) {
        this.pop3Server = pop3Server;
        init();
    }


    public PopReceivingMail(Pop3Server pop3Server, ReceiveMailSession session) {
        this.pop3Server = pop3Server;
        this.session = session;
    }

    public Pop3Server getPop3Server() {
        return pop3Server;
    }

    public void setPop3Server(Pop3Server pop3Server) {
        this.pop3Server = pop3Server;
    }

    public ReceiveMailSession getSession() {
        return session;
    }

    private void init() {
        Assert.notNull(pop3Server, "Pop3Server must not null");
        this.session = pop3Server.createSession();
    }

    public void setSession(ReceiveMailSession session) {
        this.session = session;
    }

    public ReceivedEmail[] receivedEmails() {
        Assert.notNull(session, "ReceiveMailSession must not null");
        session.open();
        return session.receiveEmail();
    }

    public static void main(String[] args) {
        String userName = "timo@storlead.com";
//        String userName = "miya@renice.co";
        String password = "Jxy19910320";
//        String password = "M159789iya";
        int port = 993;
        String host = "pop.exmail.qq.com";
        long startTime = System.currentTimeMillis();
        Pop3Server popServer = MailServer
                .create()
                .host(host)
                .auth(userName, password)
                .property("mail.imap.partialfetch","false")
                .buildPop3MailServer();

        ReceiveMailSession session = popServer.createSession();
        session.open();
        System.out.println(session.getMessageCount());
        ReceivedEmail[] emails = session.receiveEmailAndMarkSeen();
        if (emails != null) {
            for (ReceivedEmail email : emails) {

                System.out.println("\n\n===[" + email.messageNumber() + "]===");

                // common info
                System.out.println("FROM:" + email.from());
                System.out.println("TO:" + email.to()[0]);
                System.out.println("SUBJECT:" + email.subject());
                System.out.println("PRIORITY:" + email.priority());
                System.out.println("SENT DATE:" + email.sentDate());
                System.out.println("RECEIVED DATE: " + email.receivedDate());

                // process messages
                List<EmailMessage> messages = email.messages();
                for (EmailMessage msg : messages) {
                    System.out.println("------");
                    System.out.println(msg.getEncoding());
                    System.out.println(msg.getMimeType());
                    System.out.println(msg.getContent());
                }

                // process attachments
                List<EmailAttachment<? extends DataSource>> attachments = email.attachments();
                if (attachments != null) {
                    System.out.println("+++++");
                    for (EmailAttachment attachment : attachments) {
                        System.out.println("name: " + attachment.getName());
                        System.out.println("cid: " + attachment.getContentId());
                        System.out.println("size: " + attachment.getSize());
                        attachment.writeToFile(
                                new File("D:\\downFile\\mailSaveFile\\", attachment.getName()));
                    }
                }
            }

            long endTime = System.currentTimeMillis();

            System.out.println("<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>毫秒"+(endTime-startTime));
            System.out.println("<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>秒"+(endTime-startTime)/1000);
            System.out.println("<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>分"+(endTime-startTime)/1000/60);
        }
        session.close();
    }
}
